<?php
namespace Common\Lib\SMS;
use Common\Lib\Curl;

class Sms {
    protected $account;
    protected $password;
	function __construct(){
        $this->account='C99188689';
        $this->password='92f86ab3ec94441f1c3fa6399607447f';
	}
    function sms_vericode_send($mobile,$concent=''){
        $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
        if(empty($mobile)){
            exit('手机号码不能为空');
        }
        $account=$this->account;
        $password=$this->password;
        $post_data = "account={$account}&password={$password}&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$concent."。请不要把验证码泄露给其他人。");
        $gets=$this->Post($post_data, $target);
        $gets =  $this->xml_to_array($gets);
        if($gets['SubmitResult']['code']==2){
            return true;
        }else{
            return false;
        }
    }
    private function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }

//将 xml数据转换为数组格式。
    private function xml_to_array($xml){
        $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
        if(preg_match_all($reg, $xml, $matches)){
            $count = count($matches[0]);
            for($i = 0; $i < $count; $i++){
                $subxml= $matches[2][$i];
                $key = $matches[1][$i];
                if(preg_match( $reg, $subxml )){
                    $arr[$key] = $this->xml_to_array( $subxml );
                }else{
                    $arr[$key] = $subxml;
                }
            }
        }
        return $arr;
    }
    public function sms_yunxin($mobile,$code){
        include APP_PATH.'Common/Lib/SMS/dayu/smsapi.class.php';
        $uid='aa978531392';
        $pwd='admin888';
        $template='404917';
        $content = array('code'=>$code);
        //即时发送
        $api = new \SmsApi($uid,$pwd);
        $res = $api->send($mobile,$content,$template);
        if($res['stat']=='100'){
            return true;
        }else{
            return false;
        }
    }
    public function sms_dayu($mobile,$code,$type=''){
        include APP_PATH.'Common/Lib/SMS/dayu/Smsdayu.php';
        if(empty($type) || empty($mobile) || empty($code)){
            return false;
        }
        $data=array(
            'type'=>$type,
            'mobile'=>$mobile,
            'code'=>$code,
        );
        $logModel = M('log','sms_',C('DB_CONFIG2'));
        $id=$logModel->add($data);
        $demo = new \Smsdayu("LTAIBiCg1YmYyn2h", "J6KugDW1gmywhuwmDGNHEddrDv8j1r");
        $response = $demo->sendSms("诺诺信", "SMS_88115017", $mobile, array("number"=>$code,),$id);
        $data=json_encode($response);
        $data=json_decode($data,true);
        if($data['Code']=="OK"){
            $update=array(
                'requestid'=>$data['RequestId'],
                'bizid'=>$data['BizId'],
            );
            $logModel->where(array('id'=>$id))->save($update);
            return true;
        }else{
            return false;
        }
    }

    public function sms_huaxin($mobile,$code,$type=''){
        header("Content-type: text/html; charset=utf-8");
        if(empty($type) || empty($mobile) || empty($code)){
            return false;
        }
        $data=array(
            'type'=>$type,
            'mobile'=>$mobile,
            'code'=>$code,
        );
        $logModel = M('log','sms_',C('DB_CONFIG2'));
        $id=$logModel->add($data);
        $url='https://dx.ipyy.net/smsJson.aspx';
        $post_data=array(
            'userid'=>'',
            'account'=>'sz00024',
            'password'=>strtoupper(md5('spa4913')),
            //'password'=>'spa4913',
            'mobile'=>$mobile,
            'content'=>'【诺诺信】您正在操作后台登录短信验证，您的验证码：'.$code.'，请及时完成验证。如果不是本人操作，请不必理会',
            'sendTime'=>'',
            'action'=>'send',
            'extno'=>'',
        );
        //$data=curl_page($url,true,$post_data);
        $curl = new Curl();
        $data = $curl->post($url,$post_data);
        $data=json_decode($data,true);
        if($data['returnstatus']=="Success"){
            return true;
        }else{
            return false;
        }
    }


}

?>